﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using TheVerge.Base.Models;
using TheVerge.Base;
using TheVerge.Phone.Controls;
using Microsoft.Phone.Shell;
using TheVerge.Base.Platform;

namespace TheVerge.Phone.Pages
{
    public partial class ForumEntriesPage : MainPage
    {
        private ProgressIndicator loadingIndicator;
        
        private bool IsViewing;

        public ForumEntriesPage()
        {
            InitializeComponent();
            aec1.ArticleLoaded += Article_Loaded;
            DataContext = App.MainVM.Forums;

            Loaded += (s, e) => InitArticleViewer();
            OrientationChanged += (s, e) => InitArticleViewer();
        }

        #region Overrides
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            App.MainVM.ActivePinnable = App.MainVM.Forums.Active;

            if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back && aec1.IsVisible)
                aec1.Refresh();
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = IsViewing;

            if (IsViewing)
                HideArticle();
        }

        protected override void LoadFromQueryStrings(Dictionary<string, string> queryStrings)
        {
            //-- This is a little weird, but it'll actually setup the active entries.
            App.MainVM.Forums.LoadFromPin(queryStrings);
        }

        protected override bool IsMini { get { return false; } }

        protected override PageSection Section { get { return PageSection.ForumsEntries; } }
        #endregion

        #region Event Handlers
        private void Article_Click(object sender, RoutedEventArgs e)
        {
            var fe = sender as FrameworkElement;
            ShowArticle(fe.DataContext as ArticleEntry);
        }

        private void Article_Loaded(object sender, EventArgs e)
        {
            loadingIndicator.IsVisible = false;
        }

        private void thePiv_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var vm = thePiv.SelectedItem as VMForumEntries;
            if (vm != null && vm.Items.Count == 0)
                vm.Refresh();
        }
        #endregion

        #region HelperMethods
        private void HideArticle()
        {
            if (aec1.IsOverlayVisible)
            {
                aec1.HideOverlay();
            }
            else
            {
                IsViewing = loadingIndicator.IsVisible = false;
                aec1.Hide();
            }
        }

        private void InitArticleViewer()
        {
            InitLoadingIndicator();
            aec1.InitArticleViewer(forumEntriesRoot.ActualWidth, forumEntriesRoot.ActualHeight);
        }

        private void InitLoadingIndicator()
        {
            if (loadingIndicator != null) return;
            
            loadingIndicator = new ProgressIndicator();
            loadingIndicator.IsIndeterminate = true;
            loadingIndicator.Text = @"Loading . . .";
            loadingIndicator.IsVisible = false;
            SystemTray.SetProgressIndicator(this, loadingIndicator);
        }

        private void ShowArticle(ArticleEntry entry)
        {
            aec1.LoadEntry(entry);

            IsViewing = loadingIndicator.IsVisible = true;
        }
        #endregion
    }
}